<?php

namespace app\common\model\voucher;

use app\common\model\User;
use think\Model;


class Order extends Model
{





    // 表名
    protected $name = 'voucher_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'pay_time_text'
    ];






    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    static function payok($id)
    {
        $order = self::get($id);
        if ($order && !$order->is_pay) {
            $order->is_pay = 1;
            $order->pay_time = time();
            $order->save();
            User::cmoney($order->money, $order->user_id, '充值');
            $user = User::get($order->user_id);
            if ($user->group_id < $order->group_id) {
                $user->group_id = $order->group_id;
                $user->save();
            }
            // if($order->send_money){
            //     User::cmoney($order->send_money,$order->user_id,'充值赠送');
            // }
        }
    }
}
